<?php


namespace app\model\school;


use app\model\coach\UserCoachModel;
use app\model\config\ConfigOrderSourceModel;
use app\model\config\RegionModel;
use app\model\partner\EscortMenuOrderModel;
use app\model\user\UserModel;
use think\Model;
use think\model\concern\SoftDelete;

class SchoolOrderModel extends Model
{
    protected $name = 'school_order';
    protected $autoWriteTimestamp = true;

    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $append = ['status_txt'];

    // 支付失败
    const ORDER_PAY_FAIL = -1;
    // 待支付
    const ORDER_WAIT_APY = 1;
    // 待接单
    const ORDER_WAIT_ACCEPT = 2;
    // 教学中
    const ORDER_STUDYING = 3;
    // 已完成
    const ORDER_COMPLETE = 4;
    // 已失败
    const ORDER_CANCEL = 5;
    // 待验券
    const ORDER_CHECK_COUPON = 6;

    protected $statusArr = [
        -1 => '支付失败',
        1 => '待支付',
        2 => '待服务',
        3 => '服务中',
        4 => '已完成',
        5 => '已失败',
        6 => '待验券',
    ];

    public function getStatusTxtAttr($val, $data)
    {
        if (!isset($data['status'])) {
            return '';
        }
        return isset($this->statusArr[$data['status']]) ? $this->statusArr[$data['status']] : '';
    }

    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->bind([
            'user_name' => 'username',
            'user_mobile' => 'mobile',
            'user_sex' => 'sex',
            'user_avatar' => 'avatar'
        ]);
    }

    public function base()
    {
        /**
         * 参数1：远程关联表模型
         * 参数2：中间表模型
         * 参数3：远程关联表主键ID
         * 参数4：中间表主键ID
         * 参数5：当前表关联中间表的字段
         * 参数6：中间表关联远程表的字段
         */
        return $this->hasOneThrough(SchoolBaseModel::class, SchoolModel::class,
            'id', 'id', 'school_id', 'base_id');
    }

    public function school()
    {
        return $this->hasOne(SchoolModel::class, 'id', 'school_id')->bind(['school_name' => 'name', 'school_img' => 'img']);
    }

    public function course()
    {
        return $this->hasOne(SchoolCourseModel::class, 'id', 'course_id')->bind(['old_price', 'price']);
    }

    public function city()
    {
        return $this->hasOneThrough(RegionModel::class, SchoolModel::class,
            'id', 'id', 'school_id', 'city_id');
    }

    public function coach()
    {
        return $this->hasOne(UserCoachModel::class, 'id', 'coach_id')->bind([
            'coach_name' => 'username',
            'coach_mobile' => 'mobile'
        ]);
    }

    public function source()
    {
        return $this->hasOne(ConfigOrderSourceModel::class, 'id', 'source_id')->bind(['source_name' => 'title']);
    }


}